from tkinter import * 
from PIL import Image, ImageTk

class Tk_pack(Frame):
    #初始化pack
    def __init__(self, master=None):
        #Frame.__init__(self, master)
        self.frm = None
        Frame.__init__(self,self.frm)
        self.f1 =  self.pack()
        Frame.__init__(self,self.frm)
        self.f2 = self.pack()
    def loadPic1(self,path):
        self.pilImage1 = Image.open(path)
        self.pilImage1 = self.resize(200,300,self.pilImage1)
        self.tkImage1 = ImageTk.PhotoImage(image=self.pilImage1)
        return self.tkImage1
    def loadPic2(self,path):
        self.pilImage2 = Image.open(path)
        self.pilImage2 = self.resize(200,300,self.pilImage2)
        self.tkImage2 = ImageTk.PhotoImage(image=self.pilImage2)
        return self.tkImage2
    #载入图片
    def loadF1(self):
        self.label1 = Label(self.f1, text='场景图')
        self.label1.pack(side=TOP)
        self.label1 = Label(self.f1, image=self.tkImage1,bg="green",width=300,height=400)
        self.label1.pack(side=LEFT,expand=1,fill=BOTH)
    def loadF2(self):
        self.label1 = Label(self.f1, text='响应图')
        self.label1.pack(side=TOP)
        self.label2 = Label(self.f2, image=self.tkImage2,bg="pink",width=300,height=400)
        self.label2.pack(side=RIGHT,expand=1,fill=BOTH)
    #缩放图片
    def resize(self,w_box, h_box, pil_image): #参数是：要适应的窗口宽、高、Image.open后的图片
      w, h = pil_image.size #获取图像的原始大小   
      f1 = 1.0*w_box/w 
      f2 = 1.0*h_box/h    
      factor = min([f1, f2])   
      width = int(w*factor)    
      height = int(h*factor)    
      return pil_image.resize((width, height), Image.ANTIALIAS)

if __name__ == '__main__':
    PATH1 = "G:/python/img/tmp.png"
    PATH2 = "G:/python/tldk/1/home.png"
    #初始化
    root = Tk()
    root.title('自动机')
    root.geometry('800x600')

    app = Tk_pack(root)
    app.loadPic1(PATH1)
    app.loadF1()
    app.loadPic2(PATH2)
    app.loadF2()
    root.mainloop()
    